package com.leyou.test;

import com.leyou.demo.EsApplication;
import com.leyou.demo.pojo.Item;
import com.leyou.demo.repository.ItemRepository;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.metrics.avg.InternalAvg;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = EsApplication.class)
public class SelfAggs {

    @Autowired
    private ItemRepository itemRepository;

    /**
     * 聚合
     */
    @Test
    public void addAggs() {
        //
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        //添加聚合(按照词条聚合,聚合的字段是品牌)
        queryBuilder.addAggregation(AggregationBuilders.terms("brands").field("brand"));

        //通过debug可以得知,聚合得到的结果在page中,但是取值的时候需要将page转为AggregatedPage子接口中
        AggregatedPage<Item> itemPage = (AggregatedPage<Item>) this.itemRepository.search(queryBuilder.build());

        StringTerms brands = (StringTerms) itemPage.getAggregation("brands");

        List<StringTerms.Bucket> buckets = brands.getBuckets();

        buckets.forEach(bucket -> {
            Object key = bucket.getKey();
            System.out.println("key = " + key);//也就是得到了品牌的名称
            long docCount = bucket.getDocCount();
            System.out.println("docCount = " + docCount);

            System.out.println(key + "===" + docCount);

        });
    }

    /**
     * 聚合和度量(度量也可以称为是聚合)
     */
    @Test
    public void addAggsAndSub() {
        //
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        //添加聚合(按照词条聚合,聚合的字段是品牌)
        queryBuilder.addAggregation(AggregationBuilders.terms("brands").field("brand")
                .subAggregation(AggregationBuilders.avg("avg-price").field("price")));

        //通过debug可以得知,聚合得到的结果在page中,但是取值的时候需要将page转为AggregatedPage子接口中
        AggregatedPage<Item> itemPage = (AggregatedPage<Item>) this.itemRepository.search(queryBuilder.build());

        StringTerms brands = (StringTerms) itemPage.getAggregation("brands");

        List<StringTerms.Bucket> buckets = brands.getBuckets();

        buckets.forEach(bucket -> {
            //获取所有的聚合结果
            InternalAvg internalAvg = (InternalAvg) bucket.getAggregations().asMap().get("avg-price");
            //品牌
            Object key = bucket.getKey();
            //数量
            long docCount = bucket.getDocCount();

            //平均值
            double value = internalAvg.getValue();
            System.out.println(key+"品牌"+docCount+"台"+",平均价格"+value);


        });
    }
}
